package generator.handle;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.system.SystemUtil;
import freemarker.template.TemplateException;
import generator.config.ComponentConfig;
import generator.config.TableMapConfig;
import generator.config.TemplateConfig;
import generator.config.rules.TemplateConstants;
import generator.model.Bean;
import generator.util.TemplateUtils;

import java.io.File;
import java.sql.SQLException;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author angel.bin@aliyun.com
 * @since 2020-10-05 13:56
 */
public class ProcessHandle {
    ModelHandle modelHandle = new ModelHandle();
    private String projectDir = SystemUtil.getUserInfo().getCurrentDir() + "/common-generator";

    public void process(ComponentConfig componentConfig, TemplateConfig templateConfig, TableMapConfig tableMapConfig) throws SQLException, TemplateException {
        List<Bean> beanList = modelHandle.listBeans(tableMapConfig, componentConfig);
        String parent = templateConfig.getParent();
        for (Bean bean : beanList) {
            String entity = templateConfig.getEntity();
            if (StrUtil.isNotBlank(entity)) {
                bean.setEntityPackage(parent + "." + entity);
            }
            String mapper = templateConfig.getMapper();
            if (StrUtil.isNotBlank(mapper)) {
                bean.setMapperPackage(parent + "." + mapper);
            }
            String service = templateConfig.getService();
            if (StrUtil.isNotBlank(service)) {
                bean.setServicePackage(parent + "." + service);
            }
            String serviceImpl = templateConfig.getServiceImpl();
            if (StrUtil.isNotBlank(serviceImpl)) {
                bean.setServiceImplPackage(parent + "." + serviceImpl);
            }
            String controller = templateConfig.getController();
            if (StrUtil.isNotBlank(controller)) {
                bean.setControllerPackage(parent + "." + controller);
            }
            processBean(bean, templateConfig);
        }

    }

    private void processBean(Bean bean, TemplateConfig templateConfig) throws TemplateException {
        String baseDir = templateConfig.getBaseDir();
        String resourcesFile = projectDir + "/src/main/resources/";
        String java = ".java";
        String xml = ".xml";
        File baseDirFile = new File(resourcesFile + StrUtil.replace(baseDir, ".", "/"));
        if (!FileUtil.exist(baseDirFile)) {
            throw new UnsupportedOperationException("模板不存在");
        }
        String entityPackage = bean.getEntityPackage();
        if (StrUtil.isNotBlank(entityPackage)) {
            String classFile = bean.getClassName() + java;
            processJava(baseDirFile, bean, entityPackage, classFile, TemplateConstants.entity);
        }

        String mapperPackage = bean.getMapperPackage();
        if (StrUtil.isNotBlank(mapperPackage)) {
            String className = bean.getClassName();
            String mapperName = className + "Mapper";
            processJava(baseDirFile, bean, mapperPackage, mapperName + java, TemplateConstants.mapper);
            processResources(baseDirFile, bean, mapperName + xml, TemplateConstants.mapperXml);
        }

        String servicePackage = bean.getServicePackage();
        if (StrUtil.isNotBlank(servicePackage)) {
            String className = "I" + bean.getClassName() + "Service" + java;
            processJava(baseDirFile, bean, servicePackage, className, TemplateConstants.service);
        }

        String serviceImplPackage = bean.getServiceImplPackage();
        if (StrUtil.isNotBlank(serviceImplPackage)) {
            String className = bean.getClassName() + "ServiceImpl" + java;
            processJava(baseDirFile, bean, serviceImplPackage, className, TemplateConstants.serviceImpl);
        }

        String controllerPackage = bean.getControllerPackage();
        if (StrUtil.isNotBlank(controllerPackage)) {
            String className = bean.getClassName() + "Controller" + java;
            processJava(baseDirFile, bean, controllerPackage, className, TemplateConstants.controller);
        }
    }

    private void processJava(File baseDirFile, Bean bean, String pkg, String classFile, String templateName) throws TemplateException {
        String javaFile = projectDir + "/src/main/java/";
        File fullPackage = new File(javaFile + StrUtil.replace(pkg, ".", "/"));
        File outFile = new File(fullPackage, classFile);
        outFile = FileUtil.touch(outFile);
        TemplateUtils.processFile(baseDirFile, bean, outFile, templateName);
    }

    private void processResources(File baseDirFile, Bean bean, String filename, String templateName) throws TemplateException {
        String resourcesFile = projectDir + "/src/main/resources/";
        File fullPackage = new File(resourcesFile + "/mapper");
        File outFile = new File(fullPackage, filename);
        outFile = FileUtil.touch(outFile);
        TemplateUtils.processFile(baseDirFile, bean, outFile, templateName);
    }

}
